/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import framework.GameObject;
import framework.Key;
import framework.Util;
import framework.gameAPI.Sprite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.net.URL;
import sun.java2d.loops.DrawRect;

/**
 *
 * @author Ilha - 01
 */
public class Bomberman extends GameObject {

    private static final int[] ANIM_WALK_RIGHT = {0, 1, 2, 3, 4};
    private static final int[] ANIM_WALK_LEFT = {9, 8, 7, 6, 5};
    private static final int[] ANIM_WALK_UP = {9, 8, 7, 6, 5};
    private static final int[] ANIM_WALK_DOWN = {10, 11, 12, 13, 14};
    private static final int[] ANIM_STOP = {15, 16, 17};//a sequencia deve ter a mesma qtde de quadros sempre
    private static final int[] ANIM_JUMP = {3};
    private Sprite sprite;
    private int speed;
    public boolean tahColidido;

    public Bomberman(int x, int y) {
        this.sprite = new Sprite(Util.loadImage(Global.IMG_BOMBERMAN), 32, 64);
        this.x = x;
        this.y = y;
        this.sprite.setAnimationDelay(6);
        width = sprite.getWidth();
        height = sprite.getHeight();


        this.sprite.defineCollisionRectangle(0, 32, width, 32);

        this.sprite.setFrameSequence(ANIM_WALK_RIGHT);
        this.sprite.setScale(1);
        speed = 2;

    }

    @Override
    public void update() {

        this.x = this.x + this.speedX;
        int oldSpeedX = this.speedX;

        //int oldSpeedX = bomberman.getSpeedX();
        if (tahColidido) {
            this.x = this.x + 1;
            tahColidido = false;


        }
        //desfaz movimento
        this.x = this.x - oldSpeedX;

        if (Key.isDown(KeyEvent.VK_LEFT)) {
            if (!tahColidido) {
                speedX = -speed;
            }

            this.sprite.setFrameSequence(ANIM_WALK_LEFT);
        } else if (Key.isDown(KeyEvent.VK_RIGHT)) {
            speedX = speed;
            this.sprite.setFrameSequence(ANIM_WALK_RIGHT);
        } else if (Key.isDown(KeyEvent.VK_UP)) {
            speedY = -speed;
            this.sprite.setFrameSequence(ANIM_WALK_UP);
        } else if (Key.isDown(KeyEvent.VK_DOWN)) {
            speedY = speed;
            this.sprite.setFrameSequence(ANIM_WALK_DOWN);
        } else {
            speedX = 0;
            speedY = 0;
            this.sprite.setFrameSequence(ANIM_STOP);
        }

        x += speedX;
        y += speedY;
    }

    @Override
    public void paint(Graphics g) {
        this.sprite.setX(x);
        this.sprite.setY(y);
        sprite.paint(g);

        if (sprite.showColisionRect) {
            g.setColor(Color.GREEN);
            //g.drawRect(rect.x, rect.y, rect.width, rect.height);
//            g.drawRect(x, y+32, 32, 32);
            g.drawRect(x, y + 32, sprite.getRect().width, sprite.getRect().height);

        }

    }

    public Sprite getSprite() {
        return sprite;
    }

    public void setSprite(Sprite sprite) {
        this.sprite = sprite;
    }
//    @Override
//    public boolean collidesWith(GameObject obj) {
//        return super.collidesWith(obj);
//    }
}
